package com.lzh1.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


/**
 * 操作数据库的工具类
 * @author Dell
 * 
 */
public class JDBCUtils {
	
	public static Connection getConnection() throws Exception {
		//1.读取配置文件中的4个基本信息
		InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
				
		Properties pros = new Properties();
		pros.load(is);
				
		String user = pros.getProperty("user");
		String password = pros.getProperty("password");
		String url = pros.getProperty("url");
		String driverClass = pros.getProperty("driverClass");
				
		//2.加载驱动
		Class.forName(driverClass);
				
		//3.获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}
	/**
	 * 关闭连接和Statement的操作
	 * @param conn
	 * @param ps
	 * @author liuzhihao
	 * @date 2021年4月20日下午7:32:43
	 */
	public static void closeResource(Connection conn,PreparedStatement ps) {
		try {
			if(ps != null) {
				ps.close();					
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null) {
				conn.close();					
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 关闭资源操作
	 * @param conn
	 * @param ps
	 * @param rs
	 * @author liuzhihao
	 * @date 2021年4月20日下午8:36:10
	 */
	public static void closeResource(Connection conn,PreparedStatement ps,ResultSet rs) {
		try {
			if(ps != null) {
				ps.close();					
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null) {
				conn.close();					
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(rs != null) {
				rs.close();
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
